package com.example.sams.controller;

import com.example.sams.domain.request.ApplyCourseRequest;
import com.example.sams.domain.request.ChangePasswordRequest;
import com.example.sams.domain.request.UpdateTeacherProfileRequest;
import com.example.sams.service.ITeachersService;
import com.example.sams.service.IUsersService;
import com.example.sams.utils.LoginUser;
import com.example.sams.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 教师表 前端控制器
 * </p>
 *
 * @author jinghong
 * @since 2025-11-04
 */
@RestController
@RequestMapping("/teachers")
public class TeachersController {

    @Autowired
    private ITeachersService teachersService;

    @Autowired
    private IUsersService usersService;

    /**
     * 获取当前教师信息
     * GET /teachers/me
     */
    @GetMapping("/me")
    public ResultUtil getCurrentTeacherInfo() {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return teachersService.getCurrentTeacherInfo(userId);
    }

    /**
     * 更新教师信息
     * PATCH /teachers/me
     */
    @PatchMapping("/me")
    public ResultUtil updateProfile(@RequestBody UpdateTeacherProfileRequest request) {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return teachersService.updateProfile(userId, request);
    }

    /**
     * 修改密码
     * PATCH /teachers/me/password
     */
    @PatchMapping("/me/password")
    public ResultUtil changePassword(@RequestBody ChangePasswordRequest request) {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return usersService.changePassword(userId, request.getOldPassword(), request.getNewPassword());
    }

    /**
     * 获取已申请课程列表
     * GET /teachers/me/applied-courses
     */
    @GetMapping("/me/applied-courses")
    public ResultUtil getAppliedCourses() {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return teachersService.getAppliedCourses(userId);
    }

    /**
     * 申请课程
     * POST /teachers/courses/apply
     */
    @PostMapping("/courses/apply")
    public ResultUtil applyCourse(@RequestBody ApplyCourseRequest request) {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return teachersService.applyCourse(userId, request);
    }

    /**
     * 取消课程申请
     * DELETE /teachers/courses/apply/{applicationId}
     */
    @DeleteMapping("/courses/apply/{applicationId}")
    public ResultUtil cancelApplication(@PathVariable String applicationId) {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return teachersService.cancelApplication(userId, applicationId);
    }

    /**
     * 获取我的课程列表（已审核通过的）
     * GET /teachers/me/courses
     */
    @GetMapping("/me/courses")
    public ResultUtil getMyCourses() {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return teachersService.getMyCourses(userId);
    }

    /**
     * 取消课程（已审核通过的课程）
     * DELETE /teachers/courses/{courseId}
     */
    @DeleteMapping("/courses/{courseId}")
    public ResultUtil cancelCourse(@PathVariable String courseId) {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return teachersService.cancelCourse(userId, courseId);
    }

    /**
     * 获取课程学生列表
     * GET /teachers/courses/{courseId}/students
     */
    @GetMapping("/courses/{courseId}/students")
    public ResultUtil getCourseStudents(@PathVariable String courseId) {
        String userId = LoginUser.getCurrentId();
        if (userId == null) {
            return ResultUtil.error("未获取到用户信息，请重新登录");
        }
        return teachersService.getCourseStudents(userId, courseId);
    }
}
